/**
 * 地图数据
 */
var provinces = ['广东','广西','海南','贵州','云南'];
var stations500 = [];
var aclines500 = [];
var dclines500 = [];
var towerpoints500 = [];
var map;


$(window).load(function(){
	var p = $('#mapid').parent();
	$('#mapid').height(p.height());
	$.get('resources/json/china.json',function(json){
		map = new Map();
		map.registerMap(json);
		loadMapData();
	});
});
var searchObj = function(){
	var $inputSearch = $("#input_search");
	var searchSubjects = [];
	
	function addToSearchs(key,value,isTransPinYin){
		searchSubjects.push({id: key, name: value});
		if(isTransPinYin){
			var pinyinkey = pinyinUtil.getFirstLetter(key);
			searchSubjects.push({id: pinyinkey, name: value});
		};
		$inputSearch.typeahead({
			  source: searchSubjects,
			  autoSelect: false
		});
	}
	function search(data){
		map.searchObj(data);
	}
	function clear(){
		map.clearSearchObj();
	}
	return {
		addToSearchs : addToSearchs,
		search : search,
		clear : clear
	};
}();
function Map(){
	this._chart;
	this._option;
}
Map.prototype = {
		registerMap:function(mapJson){
			echarts.registerMap('china', mapJson);
			var chart = echarts.init(document.getElementById('mapid'));
			var regions = this._getRegions();
			this._option = {
				backgroundColor:'#90b9ea',
				geo:{
					map: 'china',
					roam:true,
					selectedMode:'multiple',
					center: [107, 23.8],
					zoom:4,
					silent:true,
					label :{
						show:false,
					},
					itemStyle:{
						areaColor:'#fff8f8',
						borderColor:'#fff8f8',
						borderWidth:0.5
					},
					emphasis:{
						label:{
							show:false
						},
						itemStyle:{
							areaColor:'white',
							borderColor:'#f89d52',
							borderWidth:1
						}
					},
					regions:regions
				},
				brush: {
			        brushStyle: {
			            borderWidth: 2,
			            color: 'rgba(255,255,255,0.2)',
			            borderColor: 'rgba(0,0,0,0.5)',
			        },
			        seriesIndex: [1,2,3],
			        transformable:false,
			    },
				tooltip: {
					trigger: 'item',
					triggerOn:'click',
					enterable:'true',
					padding: 0,
					hideDelay:10,
					textStyle: {
						color: '#000',
						decoration: 'none',
					},
					formatter: function(params) {
			            var tipHtml = '';
			            tipHtml = '<div style="border-radius:5px;background:#fff;box-shadow:0 0 10px 5px #aaa">' +
			                '    <div style="width:100%;border-radius:5px;background:#F8F9F9;border-bottom:1px solid #F0F0F0">' +
			                '        <span style="line-height:30px;margin:5px;">' + params.data.name + '</span>' +
			                '    </div>' +
			                '</div>';
			            return tipHtml;
			        }
				},
				series: [{
					name:'变电站',
					type:'scatter',
					coordinateSystem: 'geo',
					symbol:'image://resources/img/substation.png',
					symbolSize:10,
					data:[]
				},{
					name:'交流线',
					type:'lines',
					coordinateSystem: 'geo',
					polyline :true,
					lineStyle:{
						color:'green'
					},
					emphasis:{
						lineStyle:{
							color:'red',
							width:3,
						}
					},
					data:[],
				},{
					name:'直流线',
					type:'lines',
					coordinateSystem: 'geo',
					polyline :true,
					lineStyle:{
						color:'blue'
					},
					emphasis:{
						lineStyle:{
							color:'red',
							width:3,
						}
					},
					data:[]
				},{
					name:'双回线',
					type:'lines',
					coordinateSystem: 'geo',
					polyline :true,
					data:[]
				},{
					name:'杆塔',
					type:'scatter',
					coordinateSystem: 'geo',
					symbol:'image://resources/img/tower3.png',
					symbolSize:20,
					data:[],
			//		large:true,
					silent:true,
					animation:false
				}]
			};
			chart.setOption(this._option);
			if(stations500 && stations500.size>0){
				this.drawStation(station500);
			}
			if(towerpoints500 && towerpoints500.size>0){
				this.drawTower(towerpoints500);
			}
			if(aclines500 && aclines500.size>0){
				this.drawAcline(aclines500);
			}
			this._chart = chart;
			chart.on('brushselected', function(params){
				console.log(params);
			});
		},
		_getRegions : function(){
			var regions = [];
			for(var i in provinces){
				regions.push(getProvinceStyle(provinces[i]));
			}
			function getProvinceStyle(name){
				return {
					name: name,
					selected:true
				};
			}
			return regions;
		},
		drawStation:function(data){
			var sdata = [];
			for(var i in data){
				var d = data[i];
				sdata.push({
					name:d.name,
					id:d.id,
					value:[d.longitude,d.latitude]
				});
			}
			this._option.series[0].data = sdata;
			this._chart.setOption(this._option,true);
		},
		drawAcline:function(data){
			var sdata = [];
			for(var i in data){
				var d = data[i];
				var coords = [];
				for(var j in d.towers){
					var t = d.towers[j];
					coords.push([t.longitude,t.latitude]);
				}
				if(coords.length>3){
					sdata.push({
						name:d.lineName,
						id:d.lineId,
						coords:coords
					});
				}
			}
			this._option.series[1].data = sdata;
			this._chart.setOption(this._option,true);
		},
		drawDcline:function(data){
			var sdata = [];
			for(var i in data){
				var d = data[i];
				var coords = [];
				for(var j in d.towers){
					var t = d.towers[j];
					coords.push([t.longitude,t.latitude]);
				}
				if(coords.length>3){
					sdata.push({
						name:d.lineName,
						id:d.lineId,
						coords:coords
					});
				}
			}
			this._option.series[2].data = sdata;
			this._chart.setOption(this._option,true);
		},
		drawDoubleCircuitLine:function(data){
			
		},
		drawTower:function(data){
			var sdata = [];
			for(var i in data){
				var d = data[i];
				sdata.push({
					name:d.towerno,
					id:d.lineId+','+d.towerno,
					value:[d.longitude,d.latitude]
				});
				/*sdata.push([d.longitude,d.latitude,d.towerno]);*/
			}
		//	this._option.series[4].data = sdata;
		//	this._chart.setOption(this._option,true);
		},
		searchObj : function(data){
			this._chart.dispatchAction({
				type:'highlight',
				seriesIndex: [0,1,2],
			    name: data
			});
		},
		clearSearchObj : function(){
			this._chart.dispatchAction({
				type:'downplay',
				seriesIndex:[0,1,2]
			});
		}
};

function loadMapData(){
	$.getJSON("/rest/getLineByVoltage/500",function(result){
		aclines500 = [];
		dclines500 = [];
		for(var i in result){
			var r = result[i];
			towerpoints500 = towerpoints500.concat(r.towers);
			searchObj.addToSearchs(r.lineName,r.lineName,true);
			if(r.lineName && r.lineName.indexOf('直流')>-1){
				dclines500.push(r);
			}else{
				aclines500.push(r);
			}
		}
		if(map){
			map.drawTower(towerpoints500);
			map.drawAcline(aclines500);
			map.drawDcline(dclines500);
		}
	});
	$.getJSON("/rest/getStations/500",function(result){
		stations500 = result;
		if(map){
			map.drawStation(stations500);
		}
		for(var i in stations500){
			searchObj.addToSearchs(stations500[i].name,stations500[i].name,true);
		}
	});
}
